<?php
namespace Admin\Controller;
use Common\Controller\AuthController;
class RuleController extends AuthController {

/**
 * 显示权限管理页面
 */
    public function index(){
        $this->display();
    }
   
    /**
 * [权限权限数据]
 * @return [text] [description]
 */
    public function ruleTree(){
        $Rule=M('auth_rule');    
        $Rulelists=$Rule->order('id asc')->select();
        echo json_encode(tree($Rulelists)) ;
    }
/**
 * [添加权限]
 * @return [text] [description]
 */
    public function addRule(){
        $Rule = M("auth_rule");
        $Rule -> create(); //收集post表单数据
        $result = $Rule -> add();
        if($result){            
             echo '新建权限成功！';
        }
    }
/**
 * [删除权限]
 * @return [json] [description]
 */
public function delRule(){
    $Rule = M("auth_rule");
    $map['id']= $_POST['id'];  
    $data=$Rule->where($map)->find();

    $mapa['pid']=$_POST['id'];
    $childnum=$Rule->where($mapa)->count();

    if($childnum>0){
        $data['status']=0;
        $data['data']="不能删除包含子权限的权限！";
        echo json_encode($data);
    }else{
       $r=$Rule->where($map)->delete(); 

        if($r){
            $data['status']=1;
            $data['data']="删除权限成功！";
            echo json_encode($data);
        }
    }
      
}
/**
 * [修改权限]
 * @return [json] [description]
 */
public function modifyRule(){
    $Rule = M("auth_rule");
     $id=$_POST['id'];
        
       if($_POST['id']){

        $map['id'] = $id;

        $Rule -> create(); //收集post表单数据
           $result = $Rule->where($map)->save();
           if(false !== $result)

           echo '修改权限成功！';
           else
            echo '修改权限失败！';       
           }       
       else
       {
            echo '没有选择权限！'; 
       }
     
}
 /**
 * [西安用户组]
 * @return [json] [description]
 */
public function group(){
   $this->display();
} 
    /**
 * [用户组数据]
 * @return [text] [description]
 */
    public function groupTree(){
        $group=M('auth_group');    
        $grouplists=$group->order('id asc')->select();
        // echo count($Rulelists);
        echo json_encode($grouplists);
    }
/**
 * [添加用户组]
 * @return [text] [description]
 */
    public function add_group(){
        $group = M("auth_group");
        $group -> create(); //收集post表单数据
        $result = $group -> add();
        if($result){            
             echo '新建权限成功！';
        }
    }


/**
 * [删除用户组]
 * @return [json] [description]
 */
public function delete_group(){
    $group = M("auth_group");
    $map['id']= $_POST['id']; 
    if($_POST['id']){
        $data=$group->where($map)->find();

        $r=$group->where($map)->delete(); 

        if($r){
            $data['status']=1;
            $data['data']="删除权限成功！";
            echo json_encode($data);
        }     
    } 
}
 
/**
 * [修改用户组]
 * @return [json] [description]
 */
public function edit_group(){


    $group = M("auth_group");


     $id=$_POST['id'];
        
       if($_POST['id']){
           $map['id'] = $id;
           $data['title']=$_POST['title'];
           $data['describtion']=$_POST['describtion'];

 
           $result = $group->where($map)->save($data);
           if(false !== $result)

           echo '修改用户组成功！';
           else
            echo '修改用户组失败！';       
           }       
       else
       {
            echo '没有选择用户组！'; 
       }
     
}
public function fpRule(){

     $data=I('post.');
            $map=array(
                'id'=>$data['id']
                );
            $data['rules']=implode(',', $data['rule_ids']);
            // $result=D('AuthGroup')->editData($map,$data);
            $result = M('auth_group')->where($map)->save($data);
            if ($result) {
                $datas['status']=1;
                $datas['data']="分配权限成功！";
                echo json_encode($datas);
            }else{
                $datas['status']=0;
                $datas['data']="分配权限失败！";
                echo json_encode($datas);
            }
}

/**
 * [rule_group 显示权限列表页]
 * @return [type] [description]
 */
public function rule_group(){   
            $id=I('get.id');
            // 获取用户组数据
            $group_data=M('Auth_group')->where(array('id'=>$id))->find();
            $group_data['rules']=explode(',', $group_data['rules']);
            // 获取规则数据
             $rule_data=level(D('AuthRule')->order('id asc')->select());

            $assign=array(
                'group_data'=>$group_data,
                'rule_data'=>$rule_data
                );
            $this->assign($assign);
            $this->display();

    }
/**
 * [showadduser 添加用户到用户组界面]
 * @return [type] [description]
 */
public function showadduser(){ 
    $data['id']=I('get.id');
    $auth_group= M("auth_group"); 
    $map['id']=$data['id'];
    $data['title']=$auth_group->where($map)->getField('title'); 
    $this->assign($data);     
    $this->display();
}
/**
 * [addusertogroup 添加用户到用户组]
 * @return [type] [description]
 */
public function addusertogroup(){

    $group_access=M('auth_group_access');

    $datas['group_id']=$_POST['group_id'];
    $datas['uid']=$_POST['uid'];

 
    $cf=$group_access->where($datas)->select();
    $maps['id']=$datas['group_id'];
    $title=M("auth_group")->where($maps)->getField('title'); 
    if($cf){
        // 判断是不是已经是次管理组用户了   
        $data['status']=0;
        $data['data']='您已经是['.$title.']用户'; 
         echo json_encode($data);
    }else{
        if($_POST['group_id'] && $_POST['uid']){
            $result=$group_access->add($datas);
            if($result){
                $data['status']=1;
                $data['data']='添加用户到用户组成功';  
            }else{
                $data['status']=0;
                $data['data']='添加用户到用户组失败'; 
            }
        }else{
            $data['status']=0;
            $data['data']='添加用户到用户组失败';
        }
            echo json_encode($data);
    }
}
/**
 * [deluserfromgroup 从用户组取消用户]
 * @return [type] [description]
 */
public function deluserfromgroup(){
    $group_access=M('auth_group_access');

    $datas['group_id']=$_POST['group_id'];
    $datas['uid']=$_POST['uid'];

    $maps['id']=$datas['group_id'];
    $title=M("auth_group")->where($maps)->getField('title'); 

    $result=$group_access->where($datas)->delete();
    if($result==0){
        $data['status']=0;
        $data['data']='此用户不属于【'.$title.'】用户组，无需取消！';
    }else{
        $data['status']=1;
        $data['data']='从【'.$title.'】用户组中取消了用户！';
    }
     echo json_encode($data);
}
// 显示所有用户及其用户组数据
public function showUser(){

    $pagenum=isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rowsnum=isset($_POST['rows']) ? intval($_POST['rows']) : 10;


    $username=isset($_POST['username']) ? $_POST['username'] : '';
    $nickname=isset($_POST['nickname']) ? $_POST['nickname'] : '';

    $map['username']=array('LIKE',array("%".$username."%"));
    $map['nickname']=array('LIKE',array("%".$nickname."%"));

    $user= M("user");
    $group_access= M("auth_group_access");
    $auth_group= M("auth_group");

        $id=$_POST['id'];
         $userlist=$user->order('id asc')->where($map)->select();
         foreach ($userlist as $key => $value) {
             $map['uid']=$userlist[$key]['id'];
             $group=$group_access->where($map)->select();
             if($group){
                foreach ($group as $key1 => $value) {
                    $mapa['id']=$group[$key1]['group_id'];
                    $group[$key1]['title']=$auth_group->where($mapa)->getField('title');
                 }
                $a=array();
                foreach ($group as $key2 => $value) {
                        $a[]=$group[$key2]['title'];
                }
                $userlist[$key]['group']=implode(',', $a);
             

             }else{
                $userlist[$key]['group']="";
             }
           
            

           
         }
    $total=count($userlist);
    $json='{"total":'.$total.',"rows":'.json_encode($userlist).'}';
          echo $json ;
}     
}